import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { ObjCreate } from "../common/ObjCreate";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { KeyMapData } from "../../../common/shortcutsKey/KeyMapData";
import { KeyBoardManager } from "../../../../com/gengine/keyBoard/KeyBoardManager";
import { KeyCode } from "../../../../com/gengine/keyBoard/KeyCode";
import { ShortcutsKey } from "../../../common/shortcutsKey/ShortcutsKey";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { MsgManager } from "../../manager/MsgManager";
import { Language } from "../../../../extend/language/Language";
import { GButton } from "../../../../com/mui/controls/GButton";
import { TextInput } from "../../../../fl/controls/TextInput";
type int = number;
//class ShortcutItemRenderer
    
    export  class ShortcutItemRenderer extends GCellRenderer
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ configUI(): void
        {
            super.configUI();
            this._nameTxt = ObjCreate.createTextField("", 15, 0, 100, 29, this, GlobalStyle.textFormat1);
            this._btn = ObjCreate.createGButton("", 110, 0, 65, 20, this, "ButtonBlueStyle");
            this._btn.addEventListener(flash.events.MouseEvent.CLICK, this.onBtnClickHandler);
            this._btn.doubleClickEnabled = false;
            this._btn.focusEnabled = false;
            this._btn.mouseEnabled = true;
            this._btn.mouseChildren = false;
            this._inputTxt = ObjCreate.createGTextInput(100, 0, 65, 20, this);
            this.setInputVisible(false);
            this._inputTxt.textField.addEventListener(flash.events.FocusEvent.FOCUS_OUT, this.onInputFocusOutHandler);
            this._inputTxt.textField.addEventListener(flash.events.FocusEvent.FOCUS_IN, this.onInputfocusInHandler);
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            this._keyMapData = arg1 as KeyMapData;
            if (this._keyMapData) 
            {
                this._nameTxt.text = this._keyMapData.keyMapName;
                if (this._keyMapData.keyData) 
                {
                    this.setButtonLabel(this._keyMapData.tempName);
                }
                else 
                {
                    this.setButtonLabel();
                }
                this._btn.enabled = this._keyMapData.isCanEdit;
                this._btn.drawNow();
            }
            else 
            {
                this._nameTxt.text = "";
                this.setButtonLabel();
                this._keyMapData.tempName = "";
                this._btn.enabled = true;
            }
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._clickNum < 1) 
            {
                flash.utils.setTimeout(this.setShortcutData, 200);
            }
            var loc1;
            var loc2=((loc1 = this)._clickNum + 1);
            loc1._clickNum = loc2;
            return;
        }

        /* internal  */setShortcutData(): void
        {
            if (this._clickNum <= 1) 
            {
                this.setInputVisible(true);
            }
            else 
            {
                if (this._keyMapData.keyData) 
                {
                    this._keyMapData.tempName = this._keyMapData.keyData.shortcutsName;
                }
                this.data = this._keyMapData;
            }
            this._clickNum = 0;
            return;
        }

        /* internal  */onInputFocusOutHandler(arg1: Object /* flash.events.FocusEvent */): void
        {
            this.setInputVisible(false);
            return;
        }

        /* internal  */onInputfocusInHandler(arg1: Object /* flash.events.FocusEvent */): void
        {
            KeyBoardManager.instance.changeImeEnable(false);
            return;
        }

        /* internal  */setInputVisible(arg1: Boolean): void
        {
            this._inputTxt.visible = arg1;
            this._btn.visible = !arg1;
            if (arg1) 
            {
                this.stage.focus = this._inputTxt.textField;
                this._inputTxt.textField.text = this._btn.label;
                this.addEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.onKeyDownHandler);
                this.addEventListener(flash.events.KeyboardEvent.KEY_UP, this.onKeyDownHandler);
                this._inputTxt.textField.addEventListener(flash.events.TextEvent.TEXT_INPUT, this.onTextInputHandler);
            }
            else 
            {
                if (this._inputTxt.text == "") 
                {
                    this.setButtonLabel();
                }
                else 
                {
                    this.setButtonLabel(this._inputTxt.text);
                }
                this.removeEventListener(flash.events.KeyboardEvent.KEY_DOWN, this.onKeyDownHandler);
                this.removeEventListener(flash.events.KeyboardEvent.KEY_UP, this.onKeyDownHandler);
                this._inputTxt.textField.removeEventListener(flash.events.TextEvent.TEXT_INPUT, this.onTextInputHandler);
            }
            return;
        }

        /* internal  */onTextInputHandler(arg1: Object /* flash.events.TextEvent */): void
        {
            arg1.preventDefault();
            arg1.stopImmediatePropagation();
            return;
        }

        /* internal  */onKeyDownHandler(arg1: Object /* flash.events.KeyboardEvent */): void
        {
            var loc1=null;
            if (arg1.type != flash.events.KeyboardEvent.KEY_DOWN) 
            {
                this._tempKeyCode = 0;
            }
            else 
            {
                if (this._tempKeyCode == arg1.keyCode) 
                {
                    return;
                }
                this._tempKeyCode = arg1.keyCode;
                if (arg1.keyCode != KeyCode.BackSpace) 
                {
                    if (ShortcutsKey.instance.hasKeyCode(arg1.keyCode)) 
                    {
                        if (KeyBoardManager.ShiftKey) 
                        {
                            this.setInputTextTxt("sht+" + KeyCode.getKeyName(arg1.keyCode));
                            this.setInputVisible(false);
                        }
                        else 
                        {
                            this.setInputTextTxt(KeyCode.getKeyName(arg1.keyCode));
                            this.setInputVisible(false);
                        }
                        loc1 = ShortcutsKey.instance.getKeyMapData(arg1.keyCode, KeyBoardManager.ShiftKey);
                        if (loc1 && !(loc1 == this._keyMapData)) 
                        {
                            loc1.tempName = ShortcutItemRenderer.NoDefaultStr;
                            loc1.displayKeyData.keyCode = 0;
                            loc1.displayKeyData.isShift = false;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutsUpdate, loc1));
                            MsgManager.showRollTipsMsg(loc1.keyMapName + Language.getString(33058));
                        }
                        if (this._keyMapData) 
                        {
                            this._keyMapData.displayKeyData.isShift = KeyBoardManager.ShiftKey;
                            this._keyMapData.displayKeyData.keyCode = arg1.keyCode;
                        }
                    }
                    else if (arg1.keyCode != KeyCode.SHIFT) 
                    {
                        MsgManager.showRollTipsMsg(Language.getString(33059));
                    }
                }
                else 
                {
                    this.setInputTextTxt("");
                    if (this._keyMapData) 
                    {
                        this._keyMapData.displayKeyData.keyCode = 0;
                        this._keyMapData.displayKeyData.isShift = false;
                    }
                }
            }
            return;
        }

        /* internal  */setInputTextTxt(arg1: string): void
        {
            this._inputTxt.textField.text = arg1;
            this._inputTxt.textField.setSelection(0, (arg1.length - 1));
            return;
        }

        /* internal  */setButtonLabel(arg1: string=""): void
        {
            if (arg1 == null || arg1 == "") 
            {
                arg1 = ShortcutItemRenderer.NoDefaultStr;
            }
            this._btn.label = arg1;
            this._btn.drawNow();
            if (this._keyMapData) 
            {
                this._keyMapData.tempName = arg1;
            }
            return;
        }

        /* internal */ /* var */_nameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_btn: GButton;

        /* internal */ /* var */_inputTxt: TextInput;

        /* internal */ /* var */_keyMapData: KeyMapData;

        /* internal */ /* var */_info: Array<any>;

        /* internal */ /* var */_clickNum: number=0;

        /* internal */ /* var */_tempKeyCode: int=0;

        /* internal */ static /* var */NoDefaultStr: string;
    }

            ShortcutItemRenderer.NoDefaultStr = Language.getString(33057);
        